package com.hp.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.hp.ssm.bean.People;
import com.hp.ssm.bean.PeopleExample;
import com.hp.ssm.service.PeopleService;
import com.hp.ssm.util.JsonData;
import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/people")
public class PeopleController {

    @Resource
    private PeopleService peopleService;

    @RequestMapping("/find")
    @ResponseBody
    public Object find(String pname, @RequestParam(value = "page",defaultValue = "1") Integer page,
                                     @RequestParam(value = "limit",defaultValue = "5") Integer limit) {

        PeopleExample example=new PeopleExample();
        PeopleExample.Criteria criteria=example.createCriteria();
        if (!StringUtils.isNullOrEmpty(pname)){
            criteria.andPnameLike("%"+pname+"%");
        }
        //排序
        //未分页        List<Lessee> list=lesseeService.find(example,page,limit);
        //分页的写法
        PageInfo<People> list=peopleService.pageFind(example,page,limit);
        return JsonData.buildSuccess(list);

    }

    @RequestMapping("/findById")
    @ResponseBody
    public Object findById(Integer pid) {
        People people = peopleService.findByid(pid);
        return JsonData.buildSuccess(people);
    }

    @RequestMapping("/add")
    @ResponseBody
    public Object add(People people) {
        int i = peopleService.add(people);
        return i==1? JsonData.buildSuccess("添加成功!"): JsonData.buildError("添加失败！");
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(People people) {
        int i = peopleService.update(people);
        return i==1? JsonData.buildSuccess("修改成功!"): JsonData.buildError("修改失败！");
    }

    @RequestMapping("/del")
    @ResponseBody
    public Object del(Integer pid) {
        int i = peopleService.del(pid);
        return i==1? JsonData.buildSuccess("删除成功!"): JsonData.buildError("删除失败！");
    }

    }


